require 'rubygems' # Unless you install from the tarball or zip.
require 'icalendar'
require 'date'
#require 'test/unit'
#require 'rio'
include Icalendar # Probably do this in your class to limit namespace overlap

class FreeController < ApplicationController 
	layout 'standard'
	# list returns the information needed for free/list.rhtml
	def list
		@frees = Free.find(:all)
	end
	# show is called for each @free in free/list.rhtml and user/new.rhtml
	def show
		@free = Free.find(params[:id])
	end
	
	# new returns the information needed for free/new.rhtml
	def new
		@free = Free.new
		@user = User.find(params[:uid])
		@event = Event.find(params[:id])
	end
	# create is called when someone clicks the Create button on free/new.rhtml
	def create
		@free = Free.new(params[:free])
		@user = User.find(params[:uid])
		@event = Event.find(params[:id])
		
		@free[:user_id] = params[:uid]
		if @free.save
			redirect_to :controller => 'user', :action => 'free', :uid => @user.id, :id => @event.id
		else
			# replace this with an error
			redirect_to :action => 'error'
		end
	end
	
	def parseICS
	counter = 0
	#@meeting = Event.find(params[:id])
	#@meetingStart = @meeting[:date_begin]
	#@meetingEnd = @meeting[:date_end]
	
	#cal_file = File.open("M:/IDM/InstantRails/rails_apps/calorganizer/single_event.ics")
	@cal_file = File.open("M:/IDM/InstantRails/rails_apps/calorganizer/c091556.ics")
	# Parser returns an array of calendars because a single file can have multiple calendar objects.
	@cals = Icalendar.parse(@cal_file)
    #cal_file.close
	@cal = @cals.first
	@calsClone = @cals.dup
	#@calsClone = Marshal.load(Marshal.dump(@cals))
	@list = [34, 8, 64, 51, 32, 21]
	#testing insertion sort
	@list.each_with_index do |data,i|
     j = i - 1
     while j >= 0
       break if @list[j] <= data
       @list[j + 1] = @list[j]
       j = j - 1
     end
     @list[j + 1] = data
   end
   
	#accumulating events from all all calendars into one, called @accumulatedSchedules
	@accumulatedSchedules = Calendar.new
	@calsNum = @cals.length
	@meetingStart = DateTime.new(y=2008,m=1,d=1, h=00,min=00,s=00)
	@meetingEnd = DateTime.new(y=2010,m=1,d=1, h=00,min=00,s=00)
	
	@cals.length.times do |j|
		@tempCal = @cals[j]
		@tempCal.events.length.times do |k|
			@tempEvent = @tempCal.events[k]
			@accumulatedSchedules.add_event(@tempEvent)
		end
	end
	#sorting  (insertion sort) accumatedSchedules array according to dtstart attributes
	@accumulatedSchedules.events = @accumulatedSchedules.events.sort {|x,y| x.dtstart <=> y.dtstart}
	
	#@accumulatedSchedules.events[0].dtstart = DateTime.new(y=2010,m=1,d=1, h=00,min=00,s=00)

	#filtering out dates that are now within the time range

	##
	# parse thru all events and print these which are related to today
	#cal.events.each { |event|
	#if event.dtstart <= today && event.dtend > today
	#puts "# " + event.dtstart.to_s()[0,10] + " until " + event.dtend.to_s()[0,10]
	#puts "# " + event.summary + ", " + event.description
	#end
	#}
	##need starttime and endtime of RealEvent 
	@meetingStart = DateTime.new(y=2008,m=1,d=1, h=00,min=00,s=00)
	@meetingEnd = DateTime.new(y=2010,m=1,d=1, h=00,min=00,s=00)

	@dummy = -999
	@dummy2 = -888
	if @meetingStart < @meetingEnd
		puts @dummy
	else
		puts @dummy2
	end
	
	@cal.events.length.times do |i|
		@e = @cal.events[i]
		if (i < @cal.events.length-1)
			@nextevent = @cal.events[i+1]
		end
		
		if i == 0
			
			@temp = @e.dtstart
			@free = Free.new
			#@free[:id] = params[100]
			@free[:date_end] = @temp.to_s()
			@free[:date_begin] = @meetingStart.to_s()
			if @free.save 
				@output = i
			else 
				@output = -1
			end
		elsif i == (@cal.events.length - 1)
			@temp2 = @e.dtend
			@free = Free.new
			#@free[:id] = params[100]
			@free[:date_begin] = @temp2.to_s()
			@free[:date_end] =  @meetingEnd.to_s()
			@free.save
		else
			@free = Free.new
			#@free[:id] = params[100]
			@free[:date_begin] = @e.dtend.to_s()
			@free[:date_end] = @nextevent.dtstart.to_s()
			@free.save
		end
	end

end
end